import { reactive } from 'vue'

// 创建全局状态
export const store = reactive({
  // 购物车数据
  cart: [],
  // 愿望单数据
  wishlist: [],
  // 弹窗状态
  notification: {
    show: false,
    message: ''
  },
  // AI聊天相关状态
  aiChat: {
    isEnabled: true,
    chatHistory: [],
    // 保存聊天记录
    saveChatHistory(history) {
      this.chatHistory = history
      // 可选：保存到本地存储
      localStorage.setItem('aiChatHistory', JSON.stringify(history))
    },
    // 加载聊天记录
    loadChatHistory() {
      const saved = localStorage.getItem('aiChatHistory')
      if (saved) {
        this.chatHistory = JSON.parse(saved)
      }
      return this.chatHistory
    },
    // 清除聊天记录
    clearChatHistory() {
      this.chatHistory = []
      localStorage.removeItem('aiChatHistory')
    }
  },
  // 添加游戏到购物车
  addToCart(game) {
    // 检查游戏是否已在购物车中
    const existingGame = this.cart.find(item => item.id === game.id)
    if (!existingGame) {
      this.cart.push(game)
      this.showNotification('该游戏已添加至购物车，您可以前往商店中查看')
    }
  },
  // 添加游戏到愿望单
  addToWishlist(game) {
    // 检查游戏是否已在愿望单中
    const existingGame = this.wishlist.find(item => item.id === game.id)
    if (!existingGame) {
      this.wishlist.push(game)
      this.showNotification('该游戏已添加至愿望单')
    }
  },
  // 显示通知弹窗
  showNotification(message) {
    this.notification.message = message
    this.notification.show = true
    // 3秒后自动隐藏弹窗
    setTimeout(() => {
      this.notification.show = false
    }, 3000)
  },
  // 隐藏通知弹窗
  hideNotification() {
    this.notification.show = false
  }
})